package sever

import (
	"errors"
	"fmt"
	"gitee.com/enjoy_5/sigin-rpc/internal/model"
	"gitee.com/enjoy_5/sigin-rpc/proto/invite"
	"gorm.io/gorm"
)

func CreateInvite(in *invite.InviteCreateRequest) (*model.Invite, error) {
	stu := &model.Invite{
		InviterId: in.InviterId,
		InviteeId: in.InviteeId,
	}
	info, err := stu.GetInviteInfo()
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, err
	}
	if info.ID != 0 {
		return nil, fmt.Errorf("已经邀请该好友")
	}
	add, err := stu.CreateInvite()
	if err != nil {
		return nil, err
	}
	return add, nil
}

func GetInvite(in *invite.GetInviteRequest) ([]*model.Invite, error) {
	stu := &model.Invite{
		Model:     gorm.Model{},
		InviterId: in.InviterId,
	}
	res, err := stu.GetInvite()
	if err != nil {
		return nil, err
	}
	return res, nil
}
